package com.yq.ylmall.product.config;

import com.yq.ylmall.constant.ProductConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitProductConfig {

    @Bean(ProductConstant.PRODUCT_EXCHANGE)
    public DirectExchange productExchange() {
        return ExchangeBuilder.directExchange(ProductConstant.PRODUCT_EXCHANGE).build();
    }

    /**
     * 声明仲裁队列
     */
    @Bean(ProductConstant.PRODUCT_UPPER_QUEUE)
    public Queue productQueueByUpper() {
        return QueueBuilder.durable(ProductConstant.PRODUCT_UPPER_QUEUE)
                .quorum()
                .build();
    }

    @Bean(ProductConstant.PRODUCT_LOWER_QUEUE)
    public Queue productQueueByLower() {
        return QueueBuilder.durable(ProductConstant.PRODUCT_LOWER_QUEUE)
                .quorum()
                .build();
    }

    /**
     * 绑定交换机与队列
     */
    @Bean
    public Binding bindingUpper(){
        return BindingBuilder
                .bind(productQueueByUpper())
                .to(productExchange())
                .with(ProductConstant.PRODUCT_UPPER_KEY);
    }

    @Bean
    public Binding bindingLower(){
        return BindingBuilder
                .bind(productQueueByLower())
                .to(productExchange())
                .with(ProductConstant.PRODUCT_LOWER_KEY);
    }

}
